package de.rj.urwalking.ui.activities;

import android.content.Intent;
import android.graphics.PointF;
import android.hardware.SensorEvent;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

import de.rj.urwalking.R;
import de.rj.urwalking.config.Constants;
import de.rj.urwalking.ui.fragments.IndoorFragment;
import de.rj.urwalking.util.ExperimentManager;
import de.rj.urwalking.util.PrefUtil;
import de.rj.urwalking.util.StepDetector;
import de.rj.urwalking.util.StepDetector.StepListener;

public class IndoorActivity extends URwalkingActivity implements StepListener {

	private boolean useStepDetector;
	private StepDetector mStepDetector;
	private IndoorFragment mFragment;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_outdoor);

		initUI();
		initSensors();

		checkPreferences();
		if (useStepDetector) {
			mStepDetector = new StepDetector(this, this);
		}

	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getSupportMenuInflater();
		inflater.inflate(R.menu.indoor_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_compass:
			toggleCompass();
			break;
		case R.id.menu_experiment:
			if (ExperimentManager.getInstance().isExperimentRunning()) {
				finishExperiment(true, false);
			} else {
				startExperiment();
			}
			break;
		case R.id.menu_settings:
			startActivityForResult(new Intent(this, IndoorPreferenceActivity.class),
					Constants.PREFERENCE_ACTIVITY_REQUEST_CODE);

			break;
		default:
			return super.onOptionsItemSelected(item);
		}
		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {

		// set the state of the experiment control menu item
		MenuItem item = menu.findItem(R.id.menu_experiment);

		if (ExperimentManager.getInstance().isExperimentRunning()) {
			item.setTitle(getString(R.string.stop_experiment));
			item.setIcon(R.drawable.av_stop);
		} else {
			item.setTitle(getString(R.string.start_experiment));
			item.setIcon(R.drawable.av_play);
		}

		// set the state of the compass menu item
		item = menu.findItem(R.id.menu_compass);

		if (useCompass && mCompassIsAccurate) {
			item.setIcon(R.drawable.ic_menu_compass_active);
		} else if (useCompass && !mCompassIsAccurate) {
			item.setIcon(R.drawable.ic_menu_compass_inaccurate);
		} else {
			item.setIcon(R.drawable.ic_menu_compass);
		}

		return super.onPrepareOptionsMenu(menu);

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		switch (requestCode) {
		case Constants.QUESTIONS_ACTIVITY_REQUEST_CODE:
			break;
		case Constants.PREFERENCE_ACTIVITY_REQUEST_CODE:
			mFragment.checkPreferences();
			checkStepDetector();
			break;
		}
	}

	private void checkStepDetector() {
		if (PrefUtil.readBoolean(getString(R.string.key_pref_stepdetector), this, false)
				&& mSensorLinearAcceleration != null && mSensorRotationVector != null) {
			useStepDetector = true;
			mStepDetector = new StepDetector(this, this);

		} else {
			useStepDetector = false;
			mStepDetector.stop();
		}
	}

	@Override
	protected void onPause() {
		super.onPause();

		if (mStepDetector != null) {
			mStepDetector.stop();
		}

	}

	@Override
	protected void onResume() {
		super.onResume();
		if (useStepDetector) {
			mStepDetector = new StepDetector(this, this);
		}
	}

	protected void initUI() {
		super.initUI();
		setupActionBar();
		showIndoorFragment();
	}

	private void showIndoorFragment() {

		mFragment = new IndoorFragment();
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		ft.replace(R.id.fragment_container, mFragment);
		ft.commit();
	}

	private void setupActionBar() {
		ActionBar actionBar = getSupportActionBar();
		actionBar.setTitle(R.string.app_name);
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		super.onSensorChanged(event);

		if (useCompass) {
			mFragment.setOrientation(mOrientationZ);
		}

	}

	@Override
	public void onStep(PointF direction, float length, float variance, long timestamp) {
		mFragment.stepForward(length);
	}

	@Override
	public void onFilteredStep(Location location) {
		// not very useful indoor right now
	}

	@Override
	public void onLocationChanged(Location arg0) {
		// not very useful either
	}

	@Override
	public void switchFragments(Fragment fragment) {
		// TODO Auto-generated method stub
	}

}
